//
//  AppDelegate.swift
//  YongQingFont
//
//  Created by 李东 on 2024/4/20.
//

import UIKit
import WebKit
import IQKeyboardManagerSwift
import AppTrackingTransparency
import BUAdSDK
import AdSupport

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    var window: UIWindow?
    var bu_openId: BUSplashAd?
    var idfa = ""
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = .white
        window?.makeKeyAndVisible()
        if AppConfig.finishGuide {
            window?.rootViewController = YongQingBaseNavVC(rootViewController: YongQingMainVC())
        }else{
            window?.rootViewController = YongQingLeadVC()
        }
        IQKeyboardManager.shared.enable = true
        if let url = URL(string: "https://www.baidu.com") {
            let request = URLRequest(url: url)
            WKWebView().load(request)
        }
        APICenter.postEvent(event: .open)
        return true
    }


    func applicationDidBecomeActive(_ application: UIApplication) {
        requestIDFA()
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
//        adHelper.closeSplashView()
    }
    
    func requestIDFA() {
//        if !ADManager.initFinish {
//            return
//        }
        if #available(iOS 14, *) {
            ATTrackingManager.requestTrackingAuthorization {[weak self] status in
                if status == .authorized {
                    self?.idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
                 }
                DispatchQueue.main.async {
                    self?.tryToPresentAd()
                }
            }
        } else {
            // Fallback on earlier versions
            if ASIdentifierManager.shared().isAdvertisingTrackingEnabled {
                idfa = ASIdentifierManager.shared().advertisingIdentifier.uuidString
            }
            DispatchQueue.main.async {
                self.tryToPresentAd()
            }
        }
    }
    lazy var adHelper: NewADTools = {
        return NewADTools()
    }()
    
    
    func tryToPresentAd() {
        NewADTools.sdkInit()
        adHelper.loadAD(type: .OPEN)
    }

}

